<template>
  <div>
    <el-form
      size="mini"
      :inline="true"
      ref="article"
      :model="articleData"
      style="margin: 20px 0"
      class="demo-form-inline"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="articleData.title"
          placeholder="请输入标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="articleData.status" placeholder="请选择">
          <el-option label="已删除" value="0"></el-option>
          <el-option label="未审核" value="1"></el-option>
          <el-option label="审核通过" value="2"></el-option>
          <el-option label="审核未通过" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="resetForm('article')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="articleList" style="width: 100%" border>
      <el-table-column type="index" label="序号" width="55"> </el-table-column>
      <el-table-column prop="title" label="文章标题"> </el-table-column>
      <el-table-column prop="viewCount" label="浏览数"> </el-table-column>
      <el-table-column prop="thumhup" label="点赞数"> </el-table-column>
      <el-table-column prop="ispublic" label="是否公开">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.ispublic == 0">不公开</el-tag>
          <el-tag type="danger" v-if="scope.row.ispublic == 1">公开</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status == 0">已删除</el-tag>
          <el-tag v-if="scope.row.status == 1">未审核</el-tag>
          <el-tag type="success" v-if="scope.row.status == 2">审核通过</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 3"
            >审核未通过</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="updateDate" label="最后更新时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.status == 1"
            @click="handleOpen(scope.row)"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="danger"
            v-if="scope.row.status !== 0"
            @click="delArticle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px; text-align: center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="articleData.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="articleData.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 审核弹窗 -->
    <el-dialog
      title="审核文章"
      :visible.sync="dialogVisible"
      width="500"
    >
      <el-form ref="articleForm" :model="articleForm" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="articleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="标签" prop="labelIds">
          <el-cascader
            style="width: 100%;"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="主图" prop="imageUrl">
          <img
            style="width: 178px; height: 178px"
            :src="articleForm.imageUrl"
            alt=""
          />
        </el-form-item>
        <el-form-item label="是否公开" prop="ispublic">
          <el-radio disabled v-model="articleForm.ispublic" :label="1"
            >公开</el-radio
          >
          <el-radio disabled v-model="articleForm.ispublic" :label="0"
            >不公开</el-radio
          >
        </el-form-item>
        <el-form-item label="简介" prop="summary">
          <el-input v-model="articleForm.summary"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getArticleList, delArticle, searchArticle } from "../../api/blog";
export default {
  data() {
    return {
      dialogVisible: false,
      articleList: [],
      articleData: {
        current: 1,
        size: 10,
        title: "",
        status: "",
      },
      total: 0,
      article: {
        title: "",
        status: "",
      },
      articleForm: {},
    };
  },
  mounted() {
    this.getArticleList();
  },
  methods: {
    async getArticleList() {
      let res = await getArticleList(this.articleData);
      console.log(res);
      this.articleList = res.data.data.records;
      this.total = res.data.data.total;
    },
    //删除
    async delArticle(id) {
      let res = await delArticle(id);
      console.log(res);
      this.getArticleList();
    },
    search() {
      this.getArticleList();
      this.$refs["article"].resetFields();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSizeChange(val) {
      this.articleData.size = val;
      this.getArticleList();
    },
    handleCurrentChange(val) {
      this.articleData.page = val;
      this.getArticleList();
    },
    async handleOpen(row) {
      this.dialogVisible = true;
      let res = await searchArticle(row.id);
      this.articleForm = res.data.data;
      console.log(this.articleForm);
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
};
</script>

<style scoped >
</style>
